package anli;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * List 简单的案例 从名单中找出 缺失的学员
 */
public class Main {
    public static void main(String[] args) {
        List<Student> allStus = getStuListByPath("all.txt");
        List<Student> myStus = getStuListByPath("myStu.txt");
        List<Student> stuNotExistsList = new ArrayList<>();
        for (Student each : allStus) {
           //
            //boolean exists = contains(each, myStus); //手工打造的
            boolean exists = myStus.contains(each);
            if(!exists){
                stuNotExistsList.add(each);
            }
        }
        for (Student student : stuNotExistsList) {
            System.out.println(student);
        }
    }

    /**
     *  从文件中读取Student对象，返回List 集合
     * @param path
     * @return
     */
    private static  List<Student>  getStuListByPath(String path) {
        List<Student> allStudents = new ArrayList<>();
        Scanner sca4All = new Scanner(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));
        while (sca4All.hasNextLine()){
            String line = sca4All.nextLine();
            if(line!=null && line.trim().length()>0){
                String[] stuArr = line.split(",");
                Student student = new Student(stuArr[0], stuArr[1]);
                allStudents.add(student);
            }
        }
        sca4All.close();
        return allStudents;
    }


    /**
     * 判断集合中是否存在某个学生
     * @param one
     * @param studentList
     * @return
     */
    private static  boolean  contains(Student one, List<Student> studentList){
        boolean rs = false;
        for (Student student : studentList) {
            if(one.equals(student)){
                rs = true;
                break;
            }
        }
        return rs;
    }

}
